﻿namespace CSharpPro.properties._0930;

public class Demo
{
    public void TestPrint1()
    {
        Console.Write("请输入第一个整数 x：");
        int x = int.Parse(Console.ReadLine());

        Console.Write("请输入第二个整数 y：");
        int y = int.Parse(Console.ReadLine());

        Console.Write("请输入第三个整数 z：");
        int z = int.Parse(Console.ReadLine());

        // 使用数组排序
        int[] numbers = { x, y, z };
        Array.Sort(numbers);

        Console.WriteLine("从小到大排序结果为：");
        Console.WriteLine(string.Join(" ", numbers));

    }

    public void TestPrint2()
    {
        int a = 0;
        for (int i = 0; i < 100; i++)
        {
            if (i % 7 != 0 && i / 10 != 7 && i % 10 != 7)
            {
                Console.Write(i + " ");
                a = a + i;
            }
        }

        Console.Write(a + " ");
    }

    public void TestPrint3()
    {
        for (int i = 1; i <= 9; i++)
        {
            for (int j = 1; j <= i; j++)
            {
                Console.Write($"{j}×{i}={i * j}\t");
            }

            Console.WriteLine();
        }
    }

    public void TestPrint4()
    {
        for (int num = 2; num <= 1000; num++)
        {
            int sum = 0;
            for (int i = 1; i < num; i++)
            {
                if (num % i == 0)
                {
                    sum += i;
                }
            }

            if (sum == num)
            {
                Console.WriteLine(num);
            }
        }
    }

    public void TestPrint5()
    {
        string n = Console.ReadLine();
        int i = n.Length;
        Console.WriteLine("他是" + i + "位数");
        Console.WriteLine("逆序打印");
        for (int j = n.Length; j > 0; j--)
        {
            Console.WriteLine(n[j - 1]);
        }
    }

    public void TestPrint6()
    {
        int n;
        Console.Write("请输入空心菱形总行数（奇数）：");
        while (!int.TryParse(Console.ReadLine(), out n) || n <= 0 || (n & 1) == 0)
            Console.Write("输入无效！请重新输入一个正奇数：");
        int mid = n / 2;
        for (int i = 0; i < n; i++)
        {
            int dist = i <= mid ? mid - i : i - mid;
            int leftSpace = dist;
            for (int s = 0; s < leftSpace; s++) Console.Write(' ');
            if (i == 0 || i == n - 1)
            {
                Console.WriteLine(i == 0 ? '/' : '\\');
                continue;
            }

            int gap = 2 * (mid - dist) - 1;
            Console.Write('/');
            for (int g = 0; g < gap; g++) Console.Write(' ');
            Console.WriteLine('\\');
        }
    }

    public void TestPrint7()
    {
        Console.WriteLine("选择操作:1注册用户 2登录用户");
        int a = Convert.ToInt32(Console.ReadLine());
        if (a == 1)
        {
            const int MAX = 100; // 最多存 100 个用户
            string[] nameList = new string[MAX]; // 用户名表
            string[] pwdList = new string[MAX]; // 密码表
            int userCount = 0; // 已注册人数

            while (true) // ========== 主菜单循环 ==========
            {
                Console.Clear();
                Console.WriteLine("========== 主菜单 ==========");
                Console.WriteLine("1. 登录");
                Console.WriteLine("2. 注册");
                Console.WriteLine("3. 退出");
                Console.Write("请选择：");

                string rawSel = Console.ReadLine();
                if (rawSel == null) continue;
                rawSel = rawSel.Trim();
                if (rawSel != "1" && rawSel != "2" && rawSel != "3")
                {
                    Console.WriteLine("请输入有效数字（1-3）！");
                    Console.ReadKey();
                    continue;
                }

                if (rawSel == "3")
                {
                    Console.WriteLine("感谢使用，再见！");
                    return;
                }

                // ---------------- 注册分支 ----------------
                if (rawSel == "2")
                {
                    Console.Clear();
                    Console.WriteLine("---------- 用户注册 ----------");

                    string name, pwd, confirm;

                    // ===== 输入并校验用户名 =====
                    while (true)
                    {
                        Console.Write("请输入用户名：");
                        name = Console.ReadLine();
                        if (string.IsNullOrWhiteSpace(name))
                        {
                            Console.WriteLine("不可为空！");
                            continue;
                        }

                        name = name.Trim();
                        if (name.Length < 3 || name.Length > 15)
                        {
                            Console.WriteLine("用户名长度必须为3-15字符！");
                            continue;
                        }

                        bool badChar = false;
                        for (int i = 0; i < name.Length; i++)
                        {
                            char c = name[i];
                            if (!((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') ||
                                  c == '_'))
                            {
                                badChar = true;
                                break;
                            }
                        }

                        if (badChar)
                        {
                            Console.WriteLine("用户名只能含字母、数字、下划线！");
                            continue;
                        }

                        bool dup = false;
                        for (int i = 0; i < userCount; i++)
                            if (nameList[i] == name)
                            {
                                dup = true;
                                break;
                            }

                        if (dup)
                        {
                            Console.WriteLine("该用户名已被注册！");
                            continue;
                        }

                        break; // 用户名通过
                    }

                    // ===== 输入并校验密码 =====
                    while (true)
                    {
                        Console.Write("请输入密码：");
                        pwd = Console.ReadLine();
                        if (string.IsNullOrWhiteSpace(pwd))
                        {
                            Console.WriteLine("不可为空！");
                            continue;
                        }

                        pwd = pwd.Trim();
                        if (pwd.Length < 6 || pwd.Length > 20)
                        {
                            Console.WriteLine("密码长度必须为6-20字符！");
                            continue;
                        }

                        bool hasUpper = false, hasLower = false, hasDigit = false;
                        for (int i = 0; i < pwd.Length; i++)
                        {
                            char c = pwd[i];
                            if (c >= 'A' && c <= 'Z') hasUpper = true;
                            else if (c >= 'a' && c <= 'z') hasLower = true;
                            else if (c >= '0' && c <= '9') hasDigit = true;
                        }

                        if (!hasUpper || !hasLower || !hasDigit)
                        {
                            Console.WriteLine("密码必须包含至少1大写、1小写、1数字！");
                            continue;
                        }

                        break; // 密码通过
                    }

                    // ===== 确认密码 =====
                    while (true)
                    {
                        Console.Write("请再次输入密码：");
                        confirm = Console.ReadLine();
                        if (string.IsNullOrWhiteSpace(confirm))
                        {
                            Console.WriteLine("不可为空！");
                            continue;
                        }

                        if (confirm.Trim() != pwd)
                        {
                            Console.WriteLine("两次密码输入不一致！");
                            continue;
                        }

                        break;
                    }

                    // ===== 真正注册 =====
                    nameList[userCount] = name;
                    pwdList[userCount] = pwd;
                    userCount++;
                    Console.WriteLine("注册成功，现在可以登录了！");
                    Console.ReadKey();
                    continue;
                }

                // ---------------- 登录分支 ----------------
                if (rawSel == "1")
                {
                    Console.Clear();
                    Console.WriteLine("---------- 用户登录 ----------");

                    string name, pwd;
                    while (true)
                    {
                        Console.Write("请输入用户名：");
                        name = Console.ReadLine();
                        if (string.IsNullOrWhiteSpace(name))
                        {
                            Console.WriteLine("不可为空！");
                            continue;
                        }

                        name = name.Trim();
                        break;
                    }

                    int idx = -1;
                    for (int i = 0; i < userCount; i++)
                        if (nameList[i] == name)
                        {
                            idx = i;
                            break;
                        }

                    if (idx == -1)
                    {
                        Console.WriteLine("用户名不存在！");
                        Console.ReadKey();
                        continue;
                    }

                    while (true)
                    {
                        Console.Write("请输入密码：");
                        pwd = Console.ReadLine();
                        if (string.IsNullOrWhiteSpace(pwd))
                        {
                            Console.WriteLine("不可为空！");
                            continue;
                        }

                        break;
                    }

                    if (pwdList[idx] != pwd)
                    {
                        Console.WriteLine("密码错误！");
                        Console.ReadKey();
                        continue;
                    }

                    Console.WriteLine("登录成功！欢迎进入系统首页~");
                    Console.ReadKey();
                }
            }
        }
    }
}